﻿@model App.Web.ViewModels.ProductViewModel
@using App.Core.Extensions;
@{    
    //App.Web.ViewModels.ProductViewModel model = (App.Web.ViewModels.ProductViewModel)ViewBag.Product;
    App.Web.ViewModels.ZGL.Category.Category categoryLGL = null;

    if (Model != null && Model.CategoryZGL != null)
    {
        categoryLGL = Model.CategoryZGL;
    }
    String _Prefix = AppConstants.UI.DYNAMIC_CONTROL_PREFIX;
}
@functions{
    /// <summary>
    /// Generates Unobtrusive JS Validation attributes for dynamically generated input and selelct controls
    /// </summary>
    /// <param name="property"></param>
    /// <returns></returns>
    public MvcHtmlString GenerateValidationAttributes(App.Web.ViewModels.ZGL.Category.ProductGroupProperty property)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        ///If the field is a required field
        if (property.IsRequired.ToBoolean())
        {
            String validationMessage = property.ValidationMessage.IsNullOrEmpty() ? String.Format("Please enter {0}.", property.Label) : property.ValidationMessage;
            if (property.Option.Count > 0 || !property.ListProvider.IsNullOrEmpty())
                validationMessage = property.ValidationMessage.IsNullOrEmpty() ? String.Format("Please select {0}.", property.Label) : property.ValidationMessage;
            
            sb.AppendFormat("data-val-required=\"{0}\" ",  validationMessage);
        }
        ///If the field is a Numeric field
        if (String.Compare(property.DataType, "INT", true) == 0 || String.Compare(property.DataType, "NUM", true) == 0)
        {
            String validationMessage = property.ValidationMessage.IsNullOrEmpty() ? String.Format("Please enter numeric value for {0}.", property.Label) : property.ValidationMessage;
            if (sb.Length > 0)
                sb.Append(" ");
            sb.AppendFormat("data-val-number=\"{0}\" ", validationMessage);
            sb.AppendFormat("data-val-range-max=\"2147483647\" ", validationMessage);
            sb.AppendFormat("maxlength=\"12\" ");
        }
        if (sb.Length > 0)
        {
            sb.AppendFormat("data-val=\"true\" ");
        }
        if (sb.Length > 0)
            return MvcHtmlString.Create(String.Format(" {0}", sb.ToString()));

        return MvcHtmlString.Empty;

    }
    /// <summary>
    /// Creates the necessary attribute to show a selected value for dropdownlist. 
    /// </summary>
    /// <param name="propertyValue"></param>
    /// <param name="currentValue"></param>
    /// <returns></returns>
    public MvcHtmlString GetSelected(String propertyValue, String currentValue)
    {
        if (String.Compare(propertyValue, currentValue, true) == 0)
            return MvcHtmlString.Create(" selected=\"selected\" ");
        return MvcHtmlString.Empty;
    }
}
@if (categoryLGL != null)
{
    if (categoryLGL.Group.Count > 0)
    {
        foreach (App.Web.ViewModels.ZGL.Category.ProductGroup group in categoryLGL.Group)
        {             
            <div class="PropertyGroup">
                <div class="PropertyGroupTitle">
                    @if (!group.Icon.IsNullOrEmpty())
                    {
                        <div class="float-left">
                            <img class="ItemGroupIcon" src="@(AppConstants.Directories.PRODUCT_PROPERTY_GROUP_ICONS + "/" + group.Icon)" alt="" title="@group.Name" />
                        </div>
                    }
                    <div class="float-left ItemGroupTitle">@group.Name</div>
                    <div class="clear"></div>                
                </div>
                @foreach (App.Web.ViewModels.ZGL.Category.ProductGroupProperty property in group.Property)
                {
                    String controlName = _Prefix + property.Name;
                    MvcHtmlString validationAttributes = GenerateValidationAttributes(property);
                    MvcHtmlString validationMessageSpan = MvcHtmlString.Empty;
                    if(validationAttributes != MvcHtmlString.Empty){
                        validationMessageSpan = MvcHtmlString.Create("<span class=\"field-validation-valid\" data-valmsg-for=\"" + controlName + "\" data-valmsg-replace=\"true\"></span>");
                    }
                    <div class="editor-label">
				        @property.Label
			        </div>
			        <div class="editor-field">
                        @if (property.DataType == "BIT")
                        {
                            @:@Html.RadioButton(controlName, "Yes", property.Value.ToBoolean(), new { @id = WebUtil.GetBooleanLabelID(property.Name, true) })
                            <label for="@WebUtil.GetBooleanLabelID(property.Name, true)">Yes</label>
                            @:@Html.RadioButton(controlName, "No", !property.Value.ToBoolean(), new { @id = WebUtil.GetBooleanLabelID(property.Name, false) })
                            <label for="@WebUtil.GetBooleanLabelID(property.Name, false)">No</label>
                        }
                        ///Dropdownlist from Static Entry in ZGL
                        else if (property.Option.Count > 0)
                        { 
                            <select id="@(controlName)" name="@(controlName)"@validationAttributes>
                                <option value="">Please Select</option>
                                @foreach (var option in property.Option)
                                {
                                    <option value="@option.Value"@GetSelected(property.Value, option.Value)>@option.Text</option>
                                }
                            </select>
                            @validationMessageSpan
                        }
                        ///Dropdownlist from Database Table
                        else if (!property.ListProvider.IsNullOrEmpty() && !property.ValueColumn.IsNullOrEmpty() && !property.TextColumn.IsNullOrEmpty())
                        {
                            App.Domain.UtilityDAO dao = new App.Domain.UtilityDAO();
                            System.Data.DataTable dt = dao.GetLookUpData(property.ListProvider, property.ValueColumn, property.TextColumn);
                            <select id="@(controlName)" name="@(controlName)"@validationAttributes>
                                <option value="">Please Select</option>
                                @for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    System.Data.DataRow row = dt.Rows[i];
                                    <option value="@row[property.ValueColumn]"@GetSelected(property.Value, row[property.ValueColumn].ToString())>@row[property.TextColumn]</option>
                                }
                            </select>
                            @validationMessageSpan
                        }
                        ///Textboxes with/without validation
                        else
                        {   
                            <input type="text" class="text-box single-line" value="@property.Value.HtmlDecode()" id="@(controlName)" name="@(controlName)"@validationAttributes />
                            @validationMessageSpan
                        }
			        </div>
                }
            </div>
        }
    }
}